package com.qyhl.module_home.home.panzhou;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qyhl.module_home.R;
import com.qyhl.module_home.R2;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.constant.AppConfigConstant;
import com.qyhl.webtv.commonlib.entity.act.ActDetailsBean;
import com.qyhl.webtv.commonlib.entity.broke.ScoopTopicBean;
import com.qyhl.webtv.commonlib.entity.circle.CircleHomeBean;
import com.qyhl.webtv.commonlib.entity.circle.ScoopListBean;
import com.qyhl.webtv.commonlib.entity.home.CityBean;
import com.qyhl.webtv.commonlib.entity.home.GatherBean;
import com.qyhl.webtv.commonlib.entity.home.HomeBean;
import com.qyhl.webtv.commonlib.entity.home.NewsBean;
import com.qyhl.webtv.commonlib.entity.live.TeleTextBean;
import com.qyhl.webtv.commonlib.entity.news.AdvHomeBean;
import com.qyhl.webtv.commonlib.entity.news.AdvertiseBean;
import com.qyhl.webtv.commonlib.entity.news.GlobalNewsBean;
import com.qyhl.webtv.commonlib.entity.news.UnionBean;
import com.qyhl.webtv.commonlib.item.ItemGoodLife;
import com.qyhl.webtv.commonlib.item.ItemScoopTopic;
import com.qyhl.webtv.commonlib.item.ItemSmallVideo;
import com.qyhl.webtv.commonlib.item.panzhou.itemPanZhouAct;
import com.qyhl.webtv.commonlib.item.panzhou.itemPanZhouAdvCommon;
import com.qyhl.webtv.commonlib.item.panzhou.itemPanZhouAdvGroup;
import com.qyhl.webtv.commonlib.item.panzhou.itemPanZhouAdvLarge;
import com.qyhl.webtv.commonlib.item.panzhou.itemPanZhouCirclePic;
import com.qyhl.webtv.commonlib.item.panzhou.itemPanZhouCircleText;
import com.qyhl.webtv.commonlib.item.panzhou.itemPanZhouCircleTopic;
import com.qyhl.webtv.commonlib.item.panzhou.itemPanZhouCircleVideo;
import com.qyhl.webtv.commonlib.item.panzhou.itemPanZhouLive;
import com.qyhl.webtv.commonlib.item.panzhou.itemPanZhouNews;
import com.qyhl.webtv.commonlib.item.panzhou.itemPanZhouNewsLarge;
import com.qyhl.webtv.commonlib.item.panzhou.itemPanZhouNoPic;
import com.qyhl.webtv.commonlib.item.panzhou.itemPanZhouPic;
import com.qyhl.webtv.commonlib.item.panzhou.itemPanZhouScoop;
import com.qyhl.webtv.commonlib.item.panzhou.itemPanZhouShop;
import com.qyhl.webtv.commonlib.item.panzhou.itemPanZhouTop;
import com.qyhl.webtv.commonlib.item.panzhou.itemPanZhouUnion;
import com.qyhl.webtv.commonlib.item.panzhou.itemPanZhouUnionLarge;
import com.qyhl.webtv.commonlib.utils.ItemSkipUtils;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.commonlib.utils.action.ActionLogUtils;
import com.qyhl.webtv.commonlib.utils.config.AppConfigUtil;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.stx.xhb.androidx.XBanner;
import com.stx.xhb.androidx.transformers.Transformer;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zhy.adapter.recyclerview.wrapper.HeaderAndFooterWrapper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import es.dmoral.toasty.Toasty;

public class PanZhouListFragment extends BaseFragment implements PanZhouListContract.PanZhouListView {
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;

    private XBanner topBanner;

    private XBanner advBanner;
    private List<AdvertiseBean> advList = new ArrayList<>();

    private RecyclerView linkRecycler;
    private RelativeLayout linkLayout;

    private RecyclerView columnRecycler;

    private MultiItemTypeAdapter<GlobalNewsBean> commonAdapter;

    private HeaderAndFooterWrapper<GlobalNewsBean> headerAndFooterWrapper;

    private List<GlobalNewsBean> mDataList = new ArrayList<>();

    private CommonAdapter<CityBean.TopMenus> linkAdapter;
    private List<CityBean.TopMenus> linkList = new ArrayList<>();

    private CommonAdapter<HomeBean.SecMenus> columnAdapter;
    private List<HomeBean.SecMenus> columnList = new ArrayList<>();

    private PanZhouListPresenter mPresenter;

    private HomeBean homeBean;

    private int page = 1;//请求第x页
    private int lastNewsId = 0;//记录请求最后一条id,加载时从该id后查询
    private String lastType = "0";

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mPresenter = new PanZhouListPresenter(this);
        return inflater.inflate(R.layout.home_fragment_list_panzhou, null);
    }

    @Override
    protected void create() {
        loadMask.setStatus(LoadingLayout.Loading);

        refresh.setRefreshHeader(new MaterialHeader(getContext()));
        refresh.setRefreshFooter(new ClassicsFooter(getContext()));
        refresh.setEnableLoadMore(true);

        View headView = LayoutInflater.from(getContext()).inflate(R.layout.home_fragment_list_header_panzhou, null);
        topBanner = headView.findViewById(R.id.banner);

        advBanner = headView.findViewById(R.id.adv_banner);

        linkRecycler = headView.findViewById(R.id.link_recycler);
        linkLayout = headView.findViewById(R.id.link_layout);

        columnRecycler = headView.findViewById(R.id.column_recycler);

        recycleView.setLayoutManager(new LinearLayoutManager(getContext()));

        commonAdapter = new MultiItemTypeAdapter<>(getContext(), mDataList);
        commonAdapter.addItemViewDelegate(new itemPanZhouCircleTopic(getContext()));
        commonAdapter.addItemViewDelegate(new itemPanZhouNews(getContext()));
        commonAdapter.addItemViewDelegate(new itemPanZhouNewsLarge(getContext()));
        commonAdapter.addItemViewDelegate(new itemPanZhouPic(getContext()));
        commonAdapter.addItemViewDelegate(new itemPanZhouAct(getContext()));
        commonAdapter.addItemViewDelegate(new itemPanZhouLive(getContext()));
        commonAdapter.addItemViewDelegate(new itemPanZhouCircleTopic(getContext()));
        commonAdapter.addItemViewDelegate(new itemPanZhouNoPic());
        commonAdapter.addItemViewDelegate(new itemPanZhouScoop());
        commonAdapter.addItemViewDelegate(new itemPanZhouUnion(getContext()));
        commonAdapter.addItemViewDelegate(new itemPanZhouTop());
        commonAdapter.addItemViewDelegate(new itemPanZhouUnionLarge(getContext()));
        commonAdapter.addItemViewDelegate(new itemPanZhouAdvCommon(getContext()));
        commonAdapter.addItemViewDelegate(new itemPanZhouAdvGroup(getContext()));
        commonAdapter.addItemViewDelegate(new itemPanZhouAdvLarge(getContext()));
        commonAdapter.addItemViewDelegate(new itemPanZhouCircleText());
        commonAdapter.addItemViewDelegate(new itemPanZhouCirclePic(getContext()));
        commonAdapter.addItemViewDelegate(new itemPanZhouCircleVideo(getContext()));
        commonAdapter.addItemViewDelegate(new ItemGoodLife(getContext()));
        commonAdapter.addItemViewDelegate(new ItemSmallVideo(getContext()));
        commonAdapter.addItemViewDelegate(new itemPanZhouShop(getContext()));
        commonAdapter.addItemViewDelegate(new ItemScoopTopic());

        headerAndFooterWrapper = new HeaderAndFooterWrapper<>(commonAdapter);
        headerAndFooterWrapper.addHeaderView(headView);
        recycleView.setAdapter(headerAndFooterWrapper);

        linkRecycler.setLayoutManager(new GridLayoutManager(getContext(), 5));
        linkRecycler.setAdapter(linkAdapter = new CommonAdapter<CityBean.TopMenus>(getContext(), R.layout.home_item_link_panzhou, linkList) {
            @Override
            protected void convert(ViewHolder holder, CityBean.TopMenus bean, int position) {
                ImageView cover = holder.getView(R.id.cover);

                if (bean.getMenuName().equals("更多")) {
                    Glide.with(PanZhouListFragment.this)
                            .load(bean.getLocalImage())
                            .apply(new RequestOptions().placeholder(R.drawable.cover_normal_default).error(R.drawable.cover_normal_default))
                            .into(cover);
                } else {
                    Glide.with(PanZhouListFragment.this)
                            .load(bean.getMenuImg())
                            .apply(new RequestOptions().placeholder(R.drawable.cover_normal_default).error(R.drawable.cover_normal_default))
                            .into(cover);
                }

                holder.setText(R.id.title, bean.getMenuName());
            }
        });


        columnRecycler.setLayoutManager(new GridLayoutManager(getContext(), 3));

        columnRecycler.setAdapter(columnAdapter = new CommonAdapter<HomeBean.SecMenus>(getContext(), R.layout.home_item_column_panzhou, columnList) {
            @Override
            protected void convert(ViewHolder holder, HomeBean.SecMenus bean, int position) {
                ImageView cover = holder.getView(R.id.cover);

                Glide.with(PanZhouListFragment.this)
                        .load(bean.getMenuImg())
                        .apply(new RequestOptions().placeholder(R.drawable.cover_normal_default).error(R.drawable.cover_normal_default))
                        .into(cover);

            }
        });

        mPresenter.getConfig();
        mPresenter.getAdvData();
    }

    @Override
    protected void setListener() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                page = 1;
                lastNewsId = 0;
                lastType = "0";
                mPresenter.getConfig();
                mPresenter.getAdvData();
            }
        });

        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                lastNewsId = 0;
                lastType = "0";
                mPresenter.getConfig();
                mPresenter.getAdvData();
            }
        });

        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getGatherData(page, lastNewsId, lastType);
            }
        });

        topBanner.setOnItemClickListener(new XBanner.OnItemClickListener() {
            @Override
            public void onItemClick(XBanner banner, Object model, View view, int position) {
                HomeBean.TopNews bean = homeBean.getTopNews().get(position);
                String type = null;
                switch (bean.getType()) {
                    //活动
                    case "0":
                        if ("1".equals(bean.getActivityJson().getType())) {
                            type = "11";
                        } else {
                            type = "12";
                        }
                        break;
                    //普通
                    case "1":
                        type = "1";
                        ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.TOPNEWS);
                        break;
                    //图片
                    case "2":
                        type = "2";
                        ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.TOPNEWS);
                        break;
                    //直播
                    case "3":
                        type = "9";
                        break;
                    //标题
                    case "4":
                        type = "8";
                        ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.TOPNEWS);
                        break;
                    //视频
                    case "5":
                        type = "3";
                        ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.TOPNEWS);
                        break;
                    case "101"://抓取新闻
                        type = "10";
                        ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.TOPRECOMMENDNEWS);
                        break;
                    case "102"://小视频
                        type = "7";
                        break;
                    default:
                        break;

                }
                GlobalNewsBean globalNewsBean = new GlobalNewsBean(bean.getArticleTitle(), bean.getArticleID(), bean.getArticleURL(), bean.getArticleImg(), 0, null, bean.getType(), 0, null, bean.getCatalogID(), false, type, bean.getLivetype());
                ItemSkipUtils.getInstance().itemTopSkip(globalNewsBean, getContext());
            }
        });

        advBanner.setOnItemClickListener(new XBanner.OnItemClickListener() {
            @Override
            public void onItemClick(XBanner banner, Object model, View view, int position) {
                advSkip(advList.get(position));
            }
        });

        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder viewHolder, int position) {
                GlobalNewsBean bean = null;
                if (position >= 0) {
                    if (recycleView.getAdapter().getItemCount() == mDataList.size()) {
                        bean = mDataList.get(position);
                    } else {
                        if (position != 0) {
                            bean = mDataList.get(position - 1);
                        }
                    }
                }
                ItemSkipUtils.getInstance().itemSkip(bean, getContext(), view);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder viewHolder, int i) {
                return false;
            }
        });

        linkAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder viewHolder, int i) {
                CityBean.TopMenus bean = linkList.get(i);
                switch (bean.getTypeId()) {
                    case 1:
                        if (bean.getStyleId() == 1) {//资讯样式
                            Bundle bundle = new Bundle();
                            bundle.putString("title", bean.getMenuName());
                            bundle.putString("id", bean.getSecondId() + "");
                            bundle.putString("type", "1");
                            RouterManager.router(ARouterPathConstant.INFO, bundle);

                        } else { //节目样式

                            Bundle bundle = new Bundle();
                            bundle.putString("title", bean.getMenuName());
                            bundle.putString("menuId", bean.getSecondId() + "");
                            bundle.putString("type", "1");
                            RouterManager.router(ARouterPathConstant.PROGRAM, bundle);

                        }
                        break;
                    case 2://活动列表
                    case 16://新活动
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("title", bean.getMenuName());
                        RouterManager.router(ARouterPathConstant.ACTIVITY_LIST_TEMP, bundle);
                    }
                    break;
                    case 3://直播列表
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("title", bean.getMenuName());
                        RouterManager.router(ARouterPathConstant.LIVELIST, bundle);
                    }
                    break;
                    case 4://链接
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("title", bean.getMenuName());
                        bundle.putString("url", bean.getUrl());
                        bundle.putString("id", "");
                        int hasShare = (int) AppConfigUtil.getInstance().getConValue(AppConfigConstant.LINK_URL_SHARE);
                        if (hasShare == 1) {
                            bundle.putBoolean("hasShare", false);
                        } else {
                            bundle.putBoolean("hasShare", true);
                        }
                        RouterManager.router(ARouterPathConstant.NEWS_TITLE, bundle);
                    }
                    break;
                    case 5://电商
                        break;
                    case 6://新闻列表
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("title", bean.getMenuName());
                        bundle.putString("id", bean.getUrl());
                        RouterManager.router(ARouterPathConstant.NEWS_LIST, bundle);
                    }
                    break;
                    case 7://新趣汇
                    {
                        RouterManager.router(ARouterPathConstant.GOOD_LIFE);
                    }
                    break;
                    case 8://小视频
                    {
                        RouterManager.router(ARouterPathConstant.SMALLVIDEO);
                    }
                    break;
                    case 9:
                        switch (bean.getStyleId()) {
                            case 1://乡镇
                            {
                                Bundle bundle = new Bundle();
                                bundle.putString("title", bean.getMenuName());
                                bundle.putString("sectionId", bean.getUrl());
                                RouterManager.router(ARouterPathConstant.TOWN_LIST, bundle);
                            }
                            break;
                            case 2://政务
                            {
                                Bundle bundle = new Bundle();
                                bundle.putString("title", bean.getMenuName());
                                bundle.putString("sectionId", bean.getUrl());
                                RouterManager.router(ARouterPathConstant.GOV_LIST, bundle);
                            }

                            break;
                            case 3://专题
                            {
                                Bundle bundle = new Bundle();
                                bundle.putString("title", bean.getMenuName());
                                bundle.putString("sectionId", bean.getUrl());
                                RouterManager.router(ARouterPathConstant.SPECIAL, bundle);
                            }
                            break;
                            case 4://富顺乡镇样式
                            {
                                Bundle bundle = new Bundle();
                                bundle.putString("title", bean.getMenuName());
                                bundle.putString("sectionId", bean.getUrl());
                                RouterManager.router(ARouterPathConstant.UNION_TOWN3, bundle);
                                ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.TOWNDETAIL);
                            }
                            break;
                            default:
                                Toasty.normal(getContext(), "类型出错！", Toast.LENGTH_SHORT).show();
                                break;
                        }
                        break;
                    case 10://图文直播
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("id", bean.getUrl());
                        RouterManager.router(ARouterPathConstant.TELE_VIDEO_ACTIVITY, bundle);
                    }
                    break;
                    case 11://电视直播  1.广播 2.电视//---a
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("id", bean.getUrl());
                        bundle.putString("title", bean.getMenuName());
                        bundle.putString("type", bean.getStyleId() + "");
                        RouterManager.router(ARouterPathConstant.NEWLIVE_LIST, bundle);

                    }
                    break;
                    case 12://传统直播混合列表
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("title", bean.getMenuName());
                        bundle.putString("type", "");
                        RouterManager.router(ARouterPathConstant.MIX_LIVE, bundle);
                        ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.MIXLIVE);
                    }
                    break;
                    case 13:  //传统直播音频列表
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("title", bean.getMenuName());
                        bundle.putString("type", "2");
                        RouterManager.router(ARouterPathConstant.MIX_LIVE, bundle);
                        ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.MIXLIVE);
                    }
                    break;
                    case 14: //传统直播视频列表
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("title", bean.getMenuName());
                        bundle.putString("type", "1");
                        RouterManager.router(ARouterPathConstant.MIX_LIVE, bundle);
                        ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.MIXLIVE);
                    }
                    break;
                    case 15://新乡镇样式//---a
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("title", bean.getMenuName());
                        bundle.putString("id", bean.getUrl());
                        RouterManager.router(ARouterPathConstant.UNION_TOWN2, bundle);
                        ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.TOWNDETAIL);
                    }
                    break;
                    case 17://开票有喜
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("title", bean.getMenuName());
                        bundle.putString("id", bean.getSecondId() + "");
                        RouterManager.router(ARouterPathConstant.LUCK_DRAW, bundle);
                    }
                    break;
                    case 18://城事
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("title", bean.getMenuName());
                        RouterManager.router(ARouterPathConstant.CITY, bundle);
                    }
                    break;
                    case 19://问答
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("title", bean.getMenuName());
                        RouterManager.router(ARouterPathConstant.SCOOP_HOME, bundle);
                    }
                    break;
                    case 21://百事通
                    {
                        RouterManager.router(ARouterPathConstant.BESTONE_TYPE);
                    }
                    break;
                    case 22: {
                        Bundle bundle = new Bundle();
                        bundle.putString("title", bean.getMenuName());
                        RouterManager.router(ARouterPathConstant.NEWLIVE_LIST, bundle);
                    }
                    break;
                    case 63: {
                        boolean b = checkAppInstalled(getActivity(), "com.ss.android.ugc.aweme");
                        if (b) {
                            Intent intent = new Intent();
                            intent.setData(Uri.parse("snssdk1128://user/profile/" + bean.getUrl()));
                            startActivity(intent);
                        } else {
                            showToast("未安装此应用");
                        }
                    }
                    break;
                    default:
                        break;
                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder viewHolder, int i) {
                return false;
            }
        });


        columnAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder viewHolder, int i) {
                switch (columnList.get(i).getTypeId()) {
                    case 62:
                        Bundle bundle = new Bundle();
                        bundle.putString("title", columnList.get(i).getMenuName());
                        RouterManager.router(ARouterPathConstant.TELETEXT_LIST, bundle);
                        break;
                    case 53:
                        RouterManager.router(ARouterPathConstant.SCHOOL_HOME);
                        break;
                    case 64:
                        ARouter.getInstance()
                                .build(ARouterPathConstant.SHOP_HOME)
                                .withString("title", columnList.get(i).getMenuName())
                                .navigation();
                        break;
                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder viewHolder, int i) {
                return false;
            }
        });


    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void setConfig(HomeBean bean) {
        homeBean = bean;
        if (bean.getTopNews() != null && bean.getTopNews().size() > 0) {
            topBanner.setVisibility(View.VISIBLE);

            topBanner.setPageTransformer(Transformer.Default);
            topBanner.setBannerData(R.layout.home_item_top_banner_panzhou, bean.getTopNews());
            topBanner.loadImage(new XBanner.XBannerAdapter() {
                @Override
                public void loadBanner(XBanner banner, Object model, View view, int position) {
                    RoundedImageView bannerCover = view.findViewById(R.id.cover);
                    TextView title = view.findViewById(R.id.title);
                    title.setText(((HomeBean.TopNews) model).getArticleTitle());
                    Glide.with(PanZhouListFragment.this)
                            .load(((HomeBean.TopNews) model).getArticleImg())
                            .apply(new RequestOptions().placeholder(R.drawable.cover_normal_default).error(R.drawable.cover_normal_default))
                            .into(bannerCover);
                }
            });
        } else {
            topBanner.setVisibility(View.GONE);
        }

        linkList.clear();
        if (bean.getCityMenu() != null && bean.getCityMenu().size() > 0) {
            linkLayout.setVisibility(View.VISIBLE);

            if (bean.getCityMenu().size() <= 4) {
                linkList.addAll(bean.getCityMenu());
            } else {
                for (int i = 0; i < 4; i++) {
                    linkList.add(bean.getCityMenu().get(i));
                }
            }
            linkList.add(new CityBean.TopMenus(18, "更多", R.drawable.home_link_more_panzhou));

            linkAdapter.notifyDataSetChanged();
        } else {
            linkLayout.setVisibility(View.GONE);
        }

        columnList.clear();
        for (int i = 0; i < bean.getSecMenus().size(); i++) {
            switch (bean.getSecMenus().get(i).getTypeId()) {
                case 62:
                case 53:
                case 64:
                    columnList.add(bean.getSecMenus().get(i));
                    break;
            }
        }
        columnAdapter.notifyDataSetChanged();


        mPresenter.getGatherData(page, lastNewsId, lastType);
    }

    @Override
    public void setGatherNews(List<GatherBean> beanList, boolean isMore) {
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~~");

        if (isMore) {
            refresh.finishLoadMore();
        } else {
            mDataList.clear();
            refresh.finishRefresh();
        }

        mDataList.addAll(dataTranslate(beanList));
        headerAndFooterWrapper.notifyDataSetChanged();

        page++;
        try {  //获取最后一条新闻的id
            lastType = beanList.get(beanList.size() - 1).getType();
            switch (beanList.get(beanList.size() - 1).getType()) {
                case "5":
                    lastNewsId = beanList.get(beanList.size() - 1).getAdv().getId();
                    break;
                case "6":
                case "8":
                case "7":
                    lastNewsId = Integer.parseInt(beanList.get(beanList.size() - 1).getCatalog().getInnercode());
                    break;
                case "9":
                    lastNewsId = beanList.get(beanList.size() - 1).getLiveNews().getId();
                    break;
                case "10":
                    lastNewsId = beanList.get(beanList.size() - 1).getTopicInfo().getId();
                    break;
                case "11":
                    lastNewsId = beanList.get(beanList.size() - 1).getCircleTag().getId();
                    break;
                case "12":
                    lastNewsId = beanList.get(beanList.size() - 1).getBrokeNewsPlate().getId();
                    break;
                case "13":
                    lastNewsId = beanList.get(beanList.size() - 1).getBrokeNews().getBrokeNews().getId();
                    break;
                default:
                    lastNewsId = Integer.parseInt(beanList.get(beanList.size() - 1).getNews().getID());
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    public void showError(String msg) {
        refresh.finishRefresh();
        refresh.finishLoadMore();

        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setReloadButtonText("点击重试~~");
        loadMask.setErrorText(msg);
    }

    @Override
    public void showMessage(String msg) {
        refresh.finishRefresh();
        refresh.finishLoadMore();
        showToast(msg);
    }

    @Override
    public void showEmpty(String s) {
        refresh.finishRefresh();
        refresh.finishLoadMore();
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~~");
    }

    @Override
    public void setAdvBanner(List<AdvHomeBean> list) {
        advBanner.setVisibility(View.GONE);
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getPosition().equals("2")) {
                    advBanner.setVisibility(View.VISIBLE);

                    if (list.get(i).getAdvertise() != null && list.get(i).getAdvertise().size() > 0) {
                        advList.clear();
                        advList.addAll(list.get(i).getAdvertise());

                        advBanner.setPageTransformer(Transformer.Default);
                        advBanner.setData(R.layout.home_item_adv_banner_panzhou, advList, null);
                        advBanner.loadImage(new XBanner.XBannerAdapter() {
                            @Override
                            public void loadBanner(XBanner banner, Object model, View view, int position) {
                                RoundedImageView bannerCover = view.findViewById(R.id.cover);
                                Glide.with(PanZhouListFragment.this)
                                        .load(((AdvertiseBean) model).getImageUrl())
                                        .apply(new RequestOptions().placeholder(R.drawable.cover_normal_default).error(R.drawable.cover_normal_default))
                                        .into(bannerCover);
                            }
                        });
                    }
                }
            }
        } else {
            advBanner.setVisibility(View.GONE);
        }
    }

    private List<GlobalNewsBean> dataTranslate(List<GatherBean> beanList) {
        List<GlobalNewsBean> indexList = new ArrayList<>();
        //转换数据模型
        for (int i = 0; i < beanList.size(); i++) {
            switch (beanList.get(i).getType()) {
                case "1"://置顶新闻
                    HomeBean.TopNews bean1 = beanList.get(i).getTop();
                    String type1 = "";
                    int newsStyle = 1;
                    switch (bean1.getType()) {
                        case "0":
                            type1 = "0";
                            break;
                        case "1"://普通新闻
                            type1 = "1";
                            break;
                        case "2"://图片新闻
                            type1 = "2";
                            break;
                        case "3"://直播
                            type1 = "9";
                            break;
                        case "4"://标题新闻
                            type1 = "8";
                            break;
                        case "5"://视频新闻
                            type1 = "3";
                            break;
                        case "8"://互动直播
                            bean1.setArticleID(bean1.getRoomId());//将roomId设置成通用Id
                            type1 = "5";
                            break;
                        case "101"://抓取新闻
                            type1 = "10";
                            break;
                        case "102"://小视频
                            type1 = "7";
                            break;
                        case "JLCommonNews":
                            newsStyle = 1;
                            type1 = "13";
                            break;
                        case "JLVideoNews":
                            newsStyle = 2;
                            type1 = "13";
                            break;
                        case "JLOutNews":
                            newsStyle = 3;
                            type1 = "13";
                            break;
                        default:
                            break;
                    }
                    if ("0".equals(type1)) {
                        if ("1".equals(bean1.getActivityJson().getType())) {
                            type1 = "11";//投票活动
                        } else {
                            type1 = "12";
                        }
                    }
                    //是否是无图
                    boolean hasPicture = StringUtils.isPictureSuccess(bean1.getArticleImg());
                    indexList.add(new GlobalNewsBean(bean1.getArticleTitle(), bean1.getArticleID(), bean1.getArticleURL(), bean1.getArticleImg(), newsStyle, null, "0", 0, null, bean1.getCatalogID(), hasPicture, type1, bean1.getLivetype()));
                    break;
                case "2"://新闻类型
                    NewsBean bean2 = beanList.get(i).getNews();
                    hasPicture = StringUtils.isPictureSuccess(bean2.getLogo());
                    String type = "", pOne = "", pTwo = "", pThree = "";
                    int style = 0;
                    switch (StringUtils.isEmpty(bean2.getType()) ? "10000" : bean2.getType()) {
                        case "1"://普通新闻
                            type = "1";
                            switch (bean2.getPluralPicsFlag()) {
                                case 0:
                                    style = bean2.getCommonStyle();
                                    break;
                                case 1://三图
                                    hasPicture = false;
                                    if (bean2.getImagess().size() == 1) {
                                        hasPicture = true;
                                        pOne = bean2.getImagess().get(0).getImageUrlString();
                                    } else if (bean2.getImagess().size() == 2) {
                                        pOne = bean2.getImagess().get(0).getImageUrlString();
                                        pTwo = bean2.getImagess().get(1).getImageUrlString();
                                        hasPicture = true;
                                    } else if (bean2.getImagess().size() >= 3) {
                                        pOne = bean2.getImagess().get(0).getImageUrlString();
                                        pTwo = bean2.getImagess().get(1).getImageUrlString();
                                        pThree = bean2.getImagess().get(2).getImageUrlString();
                                        hasPicture = true;
                                    } else {
                                        hasPicture = false;
                                    }
                                    style = 4;
                                    break;
                                case 2:
                                    style = 3;
                                    break;
                                default:
                                    style = bean2.getCommonStyle();
                                    break;
                            }
                            break;
                        case "2"://图片新闻
                            type = "2";
                            if (bean2.getImagess() == null || bean2.getImagess().size() < 1) {
                                continue;
                            }
                            if (bean2.getImagess().size() == 1) {
                                hasPicture = true;
                                pOne = bean2.getImagess().get(0).getImageUrlString();
                            } else if (bean2.getImagess().size() == 2) {
                                pOne = bean2.getImagess().get(0).getImageUrlString();
                                pTwo = bean2.getImagess().get(1).getImageUrlString();
                                hasPicture = true;
                            } else if (bean2.getImagess().size() >= 3) {
                                pOne = bean2.getImagess().get(0).getImageUrlString();
                                pTwo = bean2.getImagess().get(1).getImageUrlString();
                                pThree = bean2.getImagess().get(2).getImageUrlString();
                                hasPicture = true;
                            } else {
                                hasPicture = false;
                            }
                            break;
                        case "3"://直播
                            type = "9";
                            break;
                        case "4"://标题新闻
                            type = "8";
                            break;
                        case "5"://视频新闻
                            type = "3";
                            style = bean2.getVideoStyle();
                            break;
                        case "8"://互动直播
                            type = "5";
                            bean2.setID(bean2.getRoomId());//将roomId设置成通用Id
                            break;
                        case "101"://抓取新闻
                            type = "10";
                            break;
                        default:
                            break;
                    }
                    indexList.add(new GlobalNewsBean(bean2.getTitle(), bean2.getID(), bean2.getRedirectURL(), bean2.getLogo(), style, bean2.getPublishDate(), type, Integer.parseInt(bean2.getHitCount()), bean2.getSource(), bean2.getCatalogID(), hasPicture, pOne, pTwo, pThree, bean2.getLivetype(), bean2.getCommentCount()));
                    break;
                case "3"://小视频
                    NewsBean bean3 = beanList.get(i).getNews();
                    if (bean3 != null) {
                        indexList.add(new GlobalNewsBean(bean3.getTitle(), bean3.getID(), bean3.getLink(), bean3.getLogo(), 0, bean3.getPublishDate(), "7", bean3.getCommentCount(), bean3.getSource(), bean3.getCatalogID(), true, bean3.getLivetype()));
                    }
                    break;
                case "4"://新趣汇
                    NewsBean bean4 = beanList.get(i).getNews();
                    if (bean4 != null) {
                        indexList.add(new GlobalNewsBean(bean4.getTitle(), bean4.getID(), bean4.getRedirectURL(), bean4.getLogo(), 0, bean4.getPublishDate(), "6", Integer.parseInt(StringUtils.isNotEmpty(bean4.getHitCount()) ? bean4.getHitCount() : "0"), bean4.getSource(), bean4.getCatalogID(), true, bean4.getLivetype()));
                    }
                    break;
                case "5"://广告类型
                    AdvertiseBean bean = beanList.get(i).getAdv();
                    if (bean != null) {
                        GlobalNewsBean data = new GlobalNewsBean(bean.getTitle(), bean.getId() + "", bean.getLink(), bean.getImageUrl(), Integer.parseInt(bean.getStyle()), bean.getCatalogID(), "4", 0, bean.getContentType(), bean.getLinkId(), true, bean.getLinkType());
                        data.setIsShare(bean.getIsShare());
                        indexList.add(data);
                    }
                    break;
                case "6"://乡镇
                    UnionBean unionBean = beanList.get(i).getCatalog();
                    if (unionBean != null) {
                        indexList.add(new GlobalNewsBean(unionBean.getName(), unionBean.getInnercode() + "", "", unionBean.getLogo(), beanList.get(i).getPrejectStyle(), null, "11", 0, null, unionBean.getId(), true, null));
                    }
                    break;
                case "7"://政务
                    UnionBean unionBean1 = beanList.get(i).getCatalog();
                    if (unionBean1 != null) {
                        indexList.add(new GlobalNewsBean(unionBean1.getName(), unionBean1.getInnercode() + "", "", unionBean1.getLogo(), beanList.get(i).getPrejectStyle(), null, "12", 0, null, unionBean1.getId(), true, null));
                    }
                    break;
                case "8"://专题
                    UnionBean unionBean2 = beanList.get(i).getCatalog();
                    if (unionBean2 != null) {
                        indexList.add(new GlobalNewsBean(unionBean2.getName(), unionBean2.getInnercode() + "", "", unionBean2.getLogo(), beanList.get(i).getPrejectStyle(), null, "13", 0, null, unionBean2.getId(), true, null));
                    }
                    break;
                case "9"://图文直播
                    TeleTextBean teleTextBean = beanList.get(i).getLiveNews();
                    if (teleTextBean != null) {
                        indexList.add(new GlobalNewsBean(teleTextBean.getTitle(), teleTextBean.getId() + "", teleTextBean.getType(), teleTextBean.getCoverPic(), 0, teleTextBean.getCreatetime(), "14", Integer.parseInt(teleTextBean.getHits()), teleTextBean.getStatus(), teleTextBean.getChatroomId(), true, null));
                    }
                    break;
                case "10"://圈子
                    CircleHomeBean circleHomeBean = beanList.get(i).getTopicInfo();
                    int pictureNum = 0;
                    String picOne = "", picTwo = "", picThree = "";
                    hasPicture = false;
                    if (circleHomeBean != null) {
                        String topicName = null;
                        if (circleHomeBean.getTagList() != null && circleHomeBean.getTagList().size() > 0) {
                            topicName = circleHomeBean.getTagList().get(0).getName();
                        } else {
                            topicName = "";
                        }
                        if (circleHomeBean.getPicList() != null && circleHomeBean.getPicList().size() > 0) {
                            hasPicture = true;
                            if (circleHomeBean.getPicList().size() == 1) {
                                pictureNum = 1;
                                picOne = circleHomeBean.getPicList().get(0).getThumb();
                            } else if (circleHomeBean.getPicList().size() == 2) {
                                pictureNum = 2;
                                picOne = circleHomeBean.getPicList().get(0).getThumb();
                                picTwo = circleHomeBean.getPicList().get(1).getThumb();
                            } else if (circleHomeBean.getPicList().size() == 3) {
                                pictureNum = 3;
                                picOne = circleHomeBean.getPicList().get(0).getThumb();
                                picTwo = circleHomeBean.getPicList().get(1).getThumb();
                                picThree = circleHomeBean.getPicList().get(2).getThumb();
                            } else {
                                picOne = circleHomeBean.getPicList().get(0).getThumb();
                                picTwo = circleHomeBean.getPicList().get(1).getThumb();
                                picThree = circleHomeBean.getPicList().get(2).getThumb();
                                pictureNum = circleHomeBean.getPicList().size();
                            }
                        }
                        indexList.add(new GlobalNewsBean(circleHomeBean.getContent(), circleHomeBean.getId() + "", circleHomeBean.getVideo(), circleHomeBean.getVideoThumb(), "", circleHomeBean.getType()
                                , circleHomeBean.getGmtCreate(), "15", circleHomeBean.getPageview(), topicName, circleHomeBean.getUser().getNickName(), hasPicture, "", picOne, picTwo, picThree, pictureNum));
                    }
                    break;
                case "11"://圈子话题
                    CircleHomeBean.TagList circleTag = beanList.get(i).getCircleTag();
                    if (circleTag != null) {
                        indexList.add(new GlobalNewsBean(circleTag.getName(), circleTag.getId() + "", "", circleTag.getPic(), 0,
                                "", "16", 0, circleTag.getDesc(), "", true, ""));
                    }
                    break;
                case "12"://报料栏目
                    ScoopTopicBean scoopTopicBean = beanList.get(i).getBrokeNewsPlate();
                    if (scoopTopicBean != null) {
                        indexList.add(new GlobalNewsBean(scoopTopicBean.getName(), scoopTopicBean.getId() + "", "", "", 0, "",
                                "17", 0, "", "", false, ""));
                    }
                    break;
                case "13"://报料
                    ScoopListBean scoopListBean = beanList.get(i).getBrokeNews();
                    hasPicture = false;
                    String one = "", two = "", three = "";
                    int picNum = 0;
                    if (scoopListBean != null) {
                        if (scoopListBean.getImages() != null && scoopListBean.getImages().size() > 0) {
                            hasPicture = true;
                            if (scoopListBean.getImages().size() == 1) {
                                one = scoopListBean.getImages().get(0).getThumbnailUrl();
                            } else if (scoopListBean.getImages().size() == 2) {
                                one = scoopListBean.getImages().get(0).getThumbnailUrl();
                                two = scoopListBean.getImages().get(1).getThumbnailUrl();
                            } else if (scoopListBean.getImages().size() == 3) {
                                one = scoopListBean.getImages().get(0).getThumbnailUrl();
                                two = scoopListBean.getImages().get(1).getThumbnailUrl();
                                three = scoopListBean.getImages().get(2).getThumbnailUrl();
                            } else {
                                one = scoopListBean.getImages().get(0).getThumbnailUrl();
                                two = scoopListBean.getImages().get(1).getThumbnailUrl();
                                three = scoopListBean.getImages().get(2).getThumbnailUrl();
                                picNum = scoopListBean.getImages().size();
                            }
                        }
                        indexList.add(new GlobalNewsBean(scoopListBean.getBrokeNews().getTitle(), scoopListBean.getBrokeNews().getId() + "", scoopListBean.getBrokeNews().getUrl()
                                , scoopListBean.getBrokeNews().getCoverImgUrl(), scoopListBean.getBrokeNews().getStatus() + "", scoopListBean.getBrokeNews().getType(), scoopListBean.getBrokeNews().getCreatetime(), "18"
                                , scoopListBean.getBrokeNews().getClickAmount(), scoopListBean.getBrokePlate() != null ? scoopListBean.getBrokePlate().getName() : "", scoopListBean.getBrokeNews().getContent(), hasPicture, "", one, two, three, picNum));
                    }
                    break;
                case "14"://新活动
                    ActDetailsBean actDetailsBean = beanList.get(i).getActivity();
                    int personNum;
                    try {
                        personNum = actDetailsBean.getActivity().getActivity().getPageViews().intValue();
                    } catch (Exception e) {
                        personNum = 0;
                    }
                    if (actDetailsBean != null) {
                        indexList.add(new GlobalNewsBean(actDetailsBean.getActivity().getActivity().getActName(), actDetailsBean.getActivity().getActivity().getId() + "", "", actDetailsBean.getActivity().getActivity().getPosterUrl(),
                                0, actDetailsBean.getActivity().getActivity().getCreateTime(), "19", personNum, "", actDetailsBean.getActivity().getActivity().getDigest(), true, "19"));
                    }
                    break;
                default:
                    break;
            }
        }
        return indexList;
    }

    private boolean checkAppInstalled(Context context, String pkgName) {
        if (pkgName == null || pkgName.isEmpty()) {
            return false;
        }
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> info = packageManager.getInstalledPackages(0);
        if (info == null || info.isEmpty())
            return false;
        for (int i = 0; i < info.size(); i++) {
            if (pkgName.equals(info.get(i).packageName)) {
                return true;
            }
        }
        return false;
    }

    private void advSkip(AdvertiseBean advertiseBean) {
        if (StringUtils.isEmpty(advertiseBean.getLink())) {
            switch (advertiseBean.getLinkType()) {
                case "1": {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", advertiseBean.getLinkId());
                    RouterManager.router(ARouterPathConstant.TELE_VIDEO_ACTIVITY, bundle);
                }
                break;
                case "2": {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", advertiseBean.getLinkId());
                    RouterManager.router(ARouterPathConstant.SCOOP_DETAIL_ACTIVIT, bundle);
                }
                break;
                case "3":
                    switch (advertiseBean.getContentType()) {
                        case "1": {
                            Bundle bundle = new Bundle();
                            bundle.putString("id", advertiseBean.getLinkId());
                            RouterManager.router(ARouterPathConstant.NORMAL_NEWS_ACTIVITY, bundle);
                        }
                        break;
                        case "2": {
                            Bundle bundle = new Bundle();
                            bundle.putString("id", advertiseBean.getLinkId());
                            bundle.putString("section", advertiseBean.getCatalogID());
                            RouterManager.router(ARouterPathConstant.PICTURE_NEWS_ACTIVITY, bundle);
                        }
                        break;
                        case "3":
                            break;
                        case "4":
                            break;
                        case "5": {
                            Bundle bundle = new Bundle();
                            bundle.putString("id", advertiseBean.getLinkId());
                            RouterManager.router(ARouterPathConstant.VIDEO_NEWS_ACTIVITY, bundle);
                        }
                        break;
                        case "8": {
                        }
                        break;
                        case "101": {
                            Bundle bundle = new Bundle();
                            bundle.putString("id", advertiseBean.getLinkId());
                            RouterManager.router(ARouterPathConstant.NEWS_CATCH, bundle);
                        }
                        break;
                        default:
                            break;
                    }
                    break;
                case "4": {
                    Bundle bundle = new Bundle();
                    bundle.putString("actId", advertiseBean.getLinkId());
                    RouterManager.router(ARouterPathConstant.ACT_DETAILS, bundle);
                    ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.ACTIVITY_DETAIL);
                }
                break;
                case "5": {
                    Bundle bundle = new Bundle();
                    String userName = CommonUtils.getInstance().getUserName();
                    bundle.putString("userName", userName);
                    bundle.putInt("id", Integer.parseInt(advertiseBean.getLinkId()));
                    RouterManager.routerLoginInterceptor(getContext(), bundle, ARouterPathConstant.NINE_LUCK_DRAW);
                    ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.LOTTERY);
                }
                break;
                default:

                    break;

            }
        } else {
            Bundle bundle = new Bundle();
            bundle.putString("title", advertiseBean.getTitle());
            bundle.putString("id", advertiseBean.getId() + "");
            bundle.putString("url", advertiseBean.getLink());
            bundle.putBoolean("hasJS", true);
            if (advertiseBean.getIsShare() != null && "0".equals(advertiseBean.getIsShare())) {
                bundle.putBoolean("hasShare", false);
            }
            RouterManager.router(ARouterPathConstant.ADV, bundle);
        }
        ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.ADV);
    }
}
